﻿using System.Text;
using AutoTest.Business.Dto.Bjl;
using AutoTest.Business.Dto.Xue;
using AutoTest.Business.IServices;
using AutoTest.Business.Services;
using AutoTest.Business.Vo.Xue;
using AutoTest.Common.ExcelExport;
using AutoTest.Common.Extension;
using AutoTest.Domain.Entities;
using AutoTest.Framework.Api;
using AutoTest.Framework.Pager;
using EasyTool;
using Microsoft.AspNetCore.Mvc;

namespace AutoTest.WebApi.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/xue")]
    public class XueController(IXueService xueService) : Controller
    {
        private readonly IXueService _xueService = xueService;

        #region 查询

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="searchDto"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<ApiResult<PagedList<XueListVo>>> GetXuePagedListAsync([FromQuery] XuePageSearchDto searchDto)
        {
            return await ApiResult<PagedList<XueListVo>>.Successed(() => _xueService.GetXuePagedListAsync(searchDto));
        }
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="searchDto"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<ApiResult<List<XueListVo>>> GetXueListAsync([FromQuery] XueSearchDto searchDto)
        {
            return await ApiResult<List<XueListVo>>.Successed(() => _xueService.GetXueListAsync(searchDto));
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="searchDto"></param>
        /// <returns></returns>
        [HttpGet("brief")]
        public async Task<ApiResult<List<XueBriefVo>>> GetXueBriefAsync([FromQuery] XueSearchDto searchDto)
        {
            return await ApiResult<List<XueBriefVo>>.Successed(() => _xueService.GetXueBriefListAsync(searchDto));
        }
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="searchDto"></param>
        /// <returns></returns>
        [HttpGet("export")]
        public async Task<ContentResult> ExportXueBriefAsync([FromQuery] XueSearchDto searchDto)
        {
            var dataList = await _xueService.GetXueBriefListAsync(searchDto);
            StringBuilder sb = new StringBuilder("编码,序号,轮数,结果明细");
            sb.AppendLine().AppendLine(dataList.Select(x => x.ToCsvString().StrJoin(",")).StrJoin(Environment.NewLine));
            return Content(sb.ToString(), "text/plain", Encoding.UTF8);
        }
        /// <summary>
        /// 查询明细
        /// </summary>
        /// <param name="xueId"></param>
        /// <returns></returns>
        [HttpGet("detail/{xueId}")]
        public async Task<ApiResult<XueVo>> GetXueDetailAsync([FromRoute] long xueId)
        {
            return await ApiResult<XueVo>.Successed(() => _xueService.GetXueDetail(xueId));
        }

        #endregion

        /// <summary>
        /// 同步结果
        /// </summary>
        /// <param name="createDto"></param>
        /// <returns></returns>
        [HttpPost("sync")]
        public async Task<ApiResult> SyncXueInfo([FromBody] BjlCreateDto createDto)
        {
            return await ApiResult.FromResult(() => _xueService.SyncXueInfoAsync(createDto));
        }
        /// <summary>
        /// 同步结果
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{xueId}")]
        public async Task<ApiResult> SyncXueInfo([FromRoute] long xueId)
        {
            return await ApiResult.FromResult(() => _xueService.DeleteXueAsync(xueId));
        }
    }
}
